﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrmContacto.WebApp.Code.InnerClasses.FrontEnd;
using Microsoft.Reporting.WebForms;
using Web.CrmContacto.Code;
using Web.Framework;
using CrmContacto.Contracts.Contracts;
using Web.Framework.Misc;

namespace CrmContacto.WebApp.App
{
    public partial class ReportingService : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                {
                    string reportName = this.Request.QueryString["Reporte"];

                    var reportingConfigList =  BasePage.GetReportingConfiguration(this.Application);
                    var reportingConfig = reportingConfigList.Where(r => r.Nombre == reportName).Single();

                    HasPermissionToSeeMe(reportingConfig.SecurityKey);

                    ReportingConfiguration reportingConfiguration = BasePage.GetReportingConfiguration(this.Application).Where(r => r.Nombre == reportName).First();
                    
                    this.ReportServViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
                    this.ReportServViewer.ServerReport.ReportServerUrl = new Uri(reportingConfiguration.ReportUri);
                    this.ReportServViewer.ServerReport.ReportPath = reportingConfiguration.ReportPath;
                    this.ReportServViewer.ServerReport.ReportServerCredentials = new ReportServerCredentials(reportingConfiguration.UserName, reportingConfiguration.Password, reportingConfiguration.Domain);
                    this.ReportServViewer.AsyncRendering = true;
                    this.ReportServViewer.SizeToReportContent = true;
                    this.ReportServViewer.ShowPrintButton = true;
                    this.ReportServViewer.ShowZoomControl = true;
                    //this.ReportServViewer.ZoomPercent = 130; // En IE9 se rompe

                    if (this.ReportServViewer.ServerReport.GetParameters().Where(p => p.Name == "companyId").Count() > 0)
                    {
                        ReportParameter reportParameterCompany = new ReportParameter("companyId", BasePage.CompanyId.ToString());
                        this.ReportServViewer.ServerReport.SetParameters(reportParameterCompany);
                    }

                    //this.ReportServViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
                    //this.ReportServViewer.ServerReport.ReportServerUrl = new Uri("http://svr-log:8081/ReportServer_RS");
                    //this.ReportServViewer.ServerReport.ReportPath = "/Paula Cahen D' Anvers/CRM/Crm.Ar.OperacionesPorPDVConLineas";
                    //this.ReportServViewer.ServerReport.ReportServerCredentials = new ReportServerCredentials("crmcontacto", "cc2011#1431", "vesuvio");
                    //this.ReportServViewer.AsyncRendering = true;
                    //this.ReportServViewer.SizeToReportContent = true;

                    //if (this.ReportServViewer.ServerReport.GetParameters().Where(p => p.Name == "companyId").Count() > 0)
                    //{
                    //    ReportParameter reportParameterCompany = new ReportParameter("companyId", BasePage.CompanyId.ToString());
                    //    this.ReportServViewer.ServerReport.SetParameters(reportParameterCompany);
                    //}
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}